package com.sac.approval.controller;

import com.github.pagehelper.PageInfo;
import com.sac.approval.domain.LineDO;
import com.sac.approval.domain.LineDetailDO;
import com.sac.approval.service.LineDetailService;
import com.sac.approval.service.LineService;
import com.sac.approval.vo.LineTemplateVO;
import com.sac.common.annotation.Log;
import com.sac.common.utils.R;
import com.sac.common.utils.ShiroUtils;
import com.sac.emp.domain.Employee;
import com.sac.emp.service.EmpService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 审批路径详细管理
 * @author xiaodong
 * @date 2018年9月17日 上午10:54:04
 * @desc save lines
 */
@RestController
@RequestMapping("/approval/lineDetail")
public class LineDetailController {

    @Autowired
    private LineService lineService;

    @Autowired
    private LineDetailService lineDetailService;

    @Autowired
    private EmpService empService;

    /**
     * 根据line id 查询lineDetail
     * @param lineId
     * @return
     */
    @GetMapping("/listLineDetail/{lineId}")
    public PageInfo<LineDetailDO> listLineDetail(@PathVariable String lineId) {
        Map<String, Object> params = new HashMap<>();
        params.put("lineId",lineId);
        List<LineDetailDO> lineDetailList = lineDetailService.list(params);
        lineDetailList.forEach(lineDetail->{
            Employee emp = empService.getEmpDetailInfo(lineDetail.getReceiverEpid());
            lineDetail.setName(emp.getEmpNm());
            lineDetail.setJob(emp.getEmpJob());
            lineDetail.setMail(emp.getEmpMail());
        });
        PageInfo<LineDetailDO> pageInfo = new PageInfo<>(lineDetailList);
        return pageInfo;
    }


    @Log("保存lineDetail")
    @PostMapping("/save")
    R save(LineTemplateVO lineTemplateVO) {
        LineDO line = new LineDO();
        line.setLineName(lineTemplateVO.getLineName());
        line.setIsDelete("N");
        line.setCreateUser(ShiroUtils.getEmpId().toString());
        line.setCreateTime(new Date());
        line.setUpdateTime(new Date());

        List<LineDetailDO> lineDetail = new ArrayList<>();
        if(lineTemplateVO.getAapTaplnVOList()!=null){
            Arrays.asList(lineTemplateVO.getAapTaplnVOList()).forEach(receiver -> {
                LineDetailDO templateLineDetail = new LineDetailDO();
                templateLineDetail.setType(receiver.getApRlCd());
                templateLineDetail.setReceiverEpid(receiver.getApRelrEpid());
                templateLineDetail.setSeq(receiver.getApSeq());
                templateLineDetail.setArbPmtYn(receiver.getArbPmtYn());
                templateLineDetail.setPathMdfyPmtYn(receiver.getPathMdfyPmtYn());
                templateLineDetail.setTxtMdfyPmtYn(receiver.getTxtMdfyPmtYn());
                lineDetail.add(templateLineDetail);
            });
        }

        int r= lineDetailService.saveNewLine(line,lineDetail);
        if (r > 0 ) {
            return R.ok();
        }
        return R.error();
    }

    @PutMapping("/update")
    R update(LineTemplateVO lineTemplateVO) {
        LineDO line = new LineDO();
        line.setLineId(lineTemplateVO.getLineId());
        line.setLineName(lineTemplateVO.getLineName());
        line.setUpdateTime(new Date());

        List<LineDetailDO> lineDetail = new ArrayList<>();
        if(lineTemplateVO.getAapTaplnVOList()!=null){
            Arrays.asList(lineTemplateVO.getAapTaplnVOList()).forEach(receiver -> {
                LineDetailDO templateLineDetail = new LineDetailDO();
                templateLineDetail.setLineId(lineTemplateVO.getLineId());
                templateLineDetail.setType(receiver.getApRlCd());
                templateLineDetail.setReceiverEpid(receiver.getApRelrEpid());
                templateLineDetail.setSeq(receiver.getApSeq());
                templateLineDetail.setArbPmtYn(receiver.getArbPmtYn());
                templateLineDetail.setPathMdfyPmtYn(receiver.getPathMdfyPmtYn());
                templateLineDetail.setTxtMdfyPmtYn(receiver.getTxtMdfyPmtYn());
                lineDetail.add(templateLineDetail);
            });
        }

        int r= lineDetailService.updateByLineId(line,lineDetail);
        if (r > 0 ) {
            return R.ok();
        }
        return R.error();
    }
}
